package com.seata.demo.seatauser.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQUserQueueConfig {
    public static final String USER_QUEUE_NAME="userQueue";
    public static final String USER_ROUTING_KEY="user_routing_key";
    public static final String USER_EXCHANGE_NAME="user_exchange";

    /**
     * 将普通队列绑定到交换机上
     * 声明一个持久化队列 第二个参数true为持久化，在下次重启后自动加载队列，不设置也是默认持久化
     * @return
     */
    @Bean
    public Queue userQueue() {
        return new Queue(USER_QUEUE_NAME,true);
    }

    @Bean
    public DirectExchange userExchange(){
        return new DirectExchange(USER_EXCHANGE_NAME);
    }

    @Bean
    public Binding bindingExchange(@Qualifier("userQueue")Queue queue, @Qualifier("userExchange")DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with(USER_ROUTING_KEY);
    }

}
